<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="商户Id" prop="trainingSchoolId">
        <el-input v-model="queryParams.trainingSchoolId" placeholder="请输入商户Id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="专题标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入专题标题" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="书单编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入书单编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="学期" prop="semesterId">
        <el-input v-model="queryParams.semesterId" placeholder="请输入学期" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:topic:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="topicList" border>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="商户Id" align="center" prop="trainingSchoolId"/>
      <el-table-column label="专题标题" align="center" prop="title"/>
      <el-table-column label="一封信Id" align="center" prop="letterId"/>
      <el-table-column label="书单编号" align="center" prop="code"/>
      <el-table-column label="书单类型:1, 直接销售;2, 预售;3, 预订" align="center" prop="type"/>
      <el-table-column label="学期" align="center" prop="semesterId"/>
      <el-table-column label="学校列表" align="center" prop="schoolIds"/>
      <el-table-column label="有效时间：开始" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效时间：结束" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="二维码地址" align="center" prop="qrCode"/>
      <el-table-column label="排序" align="center" prop="sortOrder"/>
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改专题书单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商户Id" prop="trainingSchoolId">
          <el-input v-model="form.trainingSchoolId" placeholder="请输入商户Id"/>
        </el-form-item>
        <el-form-item label="专题标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入专题标题"/>
        </el-form-item>
        <el-form-item label="一封信Id" prop="letterId">
          <el-input v-model="form.letterId" placeholder="请输入一封信Id"/>
        </el-form-item>
        <el-form-item label="书单编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入书单编号"/>
        </el-form-item>
        <el-form-item label="学期" prop="semesterId">
          <el-input v-model="form.semesterId" placeholder="请输入学期"/>
        </el-form-item>
        <el-form-item label="学校列表" prop="schoolIds">
          <el-input v-model="form.schoolIds" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="有效时间：开始" prop="startDate">
          <el-date-picker clearable
                          v-model="form.startDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择有效时间：开始">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效时间：结束" prop="endDate">
          <el-date-picker clearable
                          v-model="form.endDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择有效时间：结束">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否开启填写实际价格" prop="canRealPrice">
          <el-input v-model="form.canRealPrice" placeholder="请输入是否开启填写实际价格"/>
        </el-form-item>
        <el-form-item label="二维码地址" prop="qrCode">
          <el-input v-model="form.qrCode" placeholder="请输入二维码地址"/>
        </el-form-item>
        <el-form-item label="排序" prop="sortOrder">
          <el-input v-model="form.sortOrder" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
                          v-model="form.addTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除"/>
        </el-form-item>
        <el-divider content-position="center">年度书单关联的学生信息</el-divider>
        <el-table :data="ddtJiaofuStudentList" :row-class-name="rowDdtJiaofuStudentIndex" ref="ddtJiaofuStudent">
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="商户Id" prop="trainingSchoolId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.trainingSchoolId" placeholder="请输入商户Id"/>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入姓名"/>
            </template>
          </el-table-column>
          <el-table-column label="联系手机号" prop="mobile" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.mobile" placeholder="请输入联系手机号"/>
            </template>
          </el-table-column>
          <el-table-column label="身份证号" prop="idcardNo" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.idcardNo" placeholder="请输入身份证号"/>
            </template>
          </el-table-column>
          <el-table-column label="学校" prop="schoolId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.schoolId" placeholder="请输入学校"/>
            </template>
          </el-table-column>
          <el-table-column label="年级" prop="gradeId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gradeId" placeholder="请输入年级"/>
            </template>
          </el-table-column>
          <el-table-column label="班级编号" prop="classId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.classId" placeholder="请输入班级编号"/>
            </template>
          </el-table-column>
          <el-table-column label="学校名称" prop="schoolName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.schoolName" placeholder="请输入学校名称"/>
            </template>
          </el-table-column>
          <el-table-column label="年级名称" prop="gradeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gradeName" placeholder="请输入年级名称"/>
            </template>
          </el-table-column>
          <el-table-column label="班级名称" prop="className" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.className" placeholder="请输入班级名称"/>
            </template>
          </el-table-column>
          <el-table-column label="直接导入的订单价格:用于直接收款" prop="orderPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderPrice" placeholder="请输入直接导入的订单价格:用于直接收款"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="addTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.addTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间"/>
            </template>
          </el-table-column>
          <el-table-column label="逻辑删除" prop="deleted" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deleted" placeholder="请输入逻辑删除"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTopic, getTopic} from "@/api/jiaofu/topic";

export default {
  name: "Topic",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDdtJiaofuStudent: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专题书单表格数据
      topicList: [],
      // 年度书单关联的学生表格数据
      ddtJiaofuStudentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainingSchoolId: null,
        title: null,
        letterId: null,
        code: null,
        type: null,
        semesterId: null,
        schoolIds: null,
        orderType: null,
        studentType: null,
        parentType: null
      },
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专题书单列表 */
    getList() {
      this.loading = true;
      listTopic(this.queryParams).then(response => {
        this.topicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainingSchoolId: null,
        title: null,
        letterId: null,
        code: null,
        type: null,
        semesterId: null,
        schoolIds: null,
        startDate: null,
        endDate: null,
        orderType: null,
        studentType: null,
        parentType: null,
        canRealPrice: null,
        qrCode: null,
        sortOrder: null,
        addTime: null,
        updateTime: null,
        deleted: null
      };
      this.ddtJiaofuStudentList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getTopic(id).then(response => {
        this.form = response.data;
        this.ddtJiaofuStudentList = response.data.ddtJiaofuStudentList;
        this.open = true;
        this.title = "修改专题书单";
      });
    },
    /** 年度书单关联的学生序号 */
    rowDdtJiaofuStudentIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/topic/export', {
        ...this.queryParams
      }, `topic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
